#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "sqlite3.h"

#define TABLENAME "RENYIAI"
#define DBNAME "dcdn_core_v2.db"
#define DBKEY "XY*ZQB"



static int callback(void *NotUsed, int argc, char **argv, char **azColName){
    int i;
    for(i=0; i<argc; i++){
        printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
    }
    printf("\n");
    return 0;
}

int getRandChar()
{
    int ch;
    ch=rand()%100; //生成0-25的值
    ch += '!' ; //可得到一个随机字母
    return ch ;
}

void makefile(char *filename){
    int i;
    FILE *p;
    p=fopen(filename,"w");
    for(i=0;i<4096;i++)
    {
        fputc(getRandChar(),p);
    }
}

void makerootdb(sqlite3 *db, char *zErrMsg){
    time_t t=time(0);
    char sqlCommand[400];
    char filename[100];
    char filepath[100] = "/tmp/dcdn_base/dcdn_client_0/";
    int  rc;
    //  /tmp/dcdn_base/dcdn_client_0/

    sprintf(filename, "dcdn_kernel_fv2_R%lld", t);
    makefile(filename);//文件名
    strcat(filepath, filename);//filepath全路径文件名
    /*
   * tb_datafile
   * id
   * filename
     * /tmp/dcdn_base/dcdn_client_0/dcdn_kernel_fv2_105DADD4E5DA47F
     * /thunder/etc/remote.pwd
   * filesize 1073741824
   * blocksize 4194304
   * blockcnt 256
   * cv 0
   * last_ct 1400000000
   */
    sprintf (sqlCommand,\
      "INSERT INTO tbl_datafile (filename, filesize, blocksize, blockcnt, cv, last_ct) "\
      "VALUES ('%s','%d','%d','%d','%d','%d'); "\
      "INSERT INTO tbl_datafile (filename, filesize, blocksize, blockcnt, cv, last_ct) "\
      "VALUES ('%s','%d','%d','%d','%d','%d'); "\
    ,"/thunder/etc/remote.pwd", 1073741824, 4194304, 256, 0, 1400000000
    ,filepath, 1073741824, 4194304, 256, 0, 1400000000);

    /* Execute SQL statement */
    rc = sqlite3_exec(db, sqlCommand, callback, 0, &zErrMsg);
    if( rc != SQLITE_OK ){
        fprintf(stderr, "SQL error: %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
    }else{
        fprintf(stdout, "Records inserted successfully\n");
    }

}
int main(int argc, char* argv[])
{
    sqlite3 *db;
    char *zErrMsg = 0;
    int  rc;
    char *sql;
    char sqlCommand[400];
    time_t t=time(0);
    const char* data = "Callback function called";

    /* Open database */
    rc = sqlite3_open(DBNAME, &db);
    if( rc ){
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        exit(0);
    }else{
        fprintf(stdout, "Opened database successfully\n");
    }

    /* Encrypt database */
    rc = sqlite3_key(db,  DBKEY, strlen(DBKEY));
    if( rc ){
        fprintf(stderr, "Can't encrypt database: %s\n", sqlite3_errmsg(db));
        exit(0);
    }else{
        fprintf(stdout, "Database encrypted successfully\n");
    }

    /* Create SQL statement */
    sql = "CREATE TABLE RENYIAI("  \
         "ID INTEGER PRIMARY KEY AUTOINCREMENT," \
         "NAME           TEXT    NOT NULL," \
         "DATE         CHAR(50) );";

    /* Execute SQL statement */
    rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
    if( rc != SQLITE_OK ){
        fprintf(stderr, "SQL error: %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
    }else{
        fprintf(stdout, "Table created successfully\n");
    }

    makerootdb(db, zErrMsg);

    sprintf (sqlCommand,\
      "INSERT INTO RENYIAI (NAME,DATE) "\
      "VALUES ('%s','%s'); "\
            , "RENYI",ctime(&t));

    /* Execute SQL statement */
    rc = sqlite3_exec(db, sqlCommand, callback, 0, &zErrMsg);
    if( rc != SQLITE_OK ){
        fprintf(stderr, "SQL error: %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
    }else{
        fprintf(stdout, "Marks inserted successfully\n");
    }

    /* Create SQL statement */
    sql = "SELECT * from RENYIAI";

    /* Execute SQL statement */
    rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);
    if( rc != SQLITE_OK ){
        fprintf(stderr, "SQL error: %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
    }else{
        fprintf(stdout, "Operation done successfully\n");
    }

    sqlite3_close(db);
    return 0;
}